﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace Tsanie.OsuDevice.Components {
    public class DownloadEventArgs : EventArgs {
        public DownloadItem CurrentDownloadItem { get; set; }
    }
    public class DownloadMouseEventArgs : MouseEventArgs {
        public DownloadItem CurrentDownloaditem { get; set; }
        public DownloadMouseEventArgs(MouseButtons button, int clicks, int x, int y, int delta, DownloadItem item)
            : base(button, clicks, x, y, delta) {
            CurrentDownloaditem = item;
        }
    }

    public partial class Downloader : UserControl {
        private DownloadItem nowItem = null;
        private ToolStripSeparator toolStripMenuSeparator;

        public DownloadItem CurrentDownloadItem { get { return nowItem; } }
        public event EventHandler<DownloadEventArgs> CancelDownload;
        public event EventHandler<DownloadMouseEventArgs> ItemMouseDown;

        private List<DownloadItem> downloadItems = new List<DownloadItem>();
        [Description("下载项集合")]
        public List<DownloadItem> DownloadItems { get { return downloadItems; } }
        private ToolStripMenuItem[] menuItems = new ToolStripMenuItem[0];
        public ToolStripMenuItem[] MenuItems {
            get { return menuItems; }
            set {
                if (value == null)
                    menuItems = new ToolStripMenuItem[0];
                foreach (ToolStripItem item in contextMenu.Items) {
                    if (item != toolStripMenuCancel && item != toolStripMenuSeparator)
                        contextMenu.Items.Remove(item);
                }
                int i = 0;
                for (; i < value.Length; i++) {
                    contextMenu.Items.Insert(i, value[i]);
                }
                if (value.Length > 0)
                    contextMenu.Items.Insert(i, toolStripMenuSeparator);
                menuItems = value;
            }
        }

        public DownloadItem AddDownload(string filename, ListViewItem lvi) {
            DownloadItem item = new DownloadItem(lvi) { FileName = filename, FileIcon = Tsanie.OsuDevice.Properties.Resources.osu };
            item.ContextMenuStrip = contextMenu;
            item.MouseDown += new MouseEventHandler(item_MouseDown);
            downloadItems.Add(item);
            this.Controls.Add(item);
            item.BringToFront();
            return item;
        }

        private void item_MouseDown(object sender, MouseEventArgs e) {
            nowItem = sender as DownloadItem;
            nowItem.SetFocus();
            if (ItemMouseDown != null)
                ItemMouseDown(this, new DownloadMouseEventArgs(e.Button, e.Clicks, e.X, e.Y, e.Delta, nowItem));
        }

        public void RemoveDownload(DownloadItem item) {
            int i = 0;
            for (; i < downloadItems.Count; i++) {
                if (downloadItems[i] == item)
                    break;
            }
            this.Controls.Remove(item);
            downloadItems.RemoveAt(i);
            if (downloadItems.Count == 0) {
                this.Focus();
            } else {
                if (i >= downloadItems.Count)
                    i = downloadItems.Count - 1;
                downloadItems[i].SetFocus();
            }
        }

        public Downloader() {
            InitializeComponent();
            this.toolStripMenuSeparator = new ToolStripSeparator();
            this.toolStripMenuSeparator.Name = "toolStripMenuSeperator";
        }

        private void toolStripMenuCancel_Click(object sender, EventArgs e) {
            if (nowItem != null && CancelDownload != null) {
                CancelDownload(this, new DownloadEventArgs() { CurrentDownloadItem = nowItem });
            }
        }
    }
}
